<script setup lang="ts" name="OidcAuth">
// import { notification } from 'ant-design-vue'
import * as service from "@/api/oidc";
import keycloak from "@/utils/sgmw_auth/keycloak";
// import KeycloakPlugin from "@/utils/sgmw_auth/keycloakPlug";
// import { requestGet, requestPost } from "@/utils/sgmw_auth/http";

import { $t } from "@/plugins/i18n";
import { useRoute } from "vue-router";
import { type DataInfo, setToken, removeToken, userKey } from "@/utils/auth";

const route = useRoute();
// const { t } = useI18nLocale()

function accessToken() {
  const code = route.query.code as string;
  console.log(code);

  service.accessToken(code).then((res_token: any) => {
    let chulitoken = {
      accessToken: res_token.access_token,
      refreshToken: res_token.refresh_token,
      expires: new Date(),
      username: "",
      roles: ["admin"]
    };
    // let token = res_token.access_token;
    // alert(chulitoken);
    console.log(chulitoken.accessToken);
    console.log(chulitoken.refreshToken);
    // setToken(token);
    service
      .getUserInfo(chulitoken.accessToken)
      .then((res: any) => {
        chulitoken.username = res.family_name + res.given_name;
        setToken(chulitoken);
        // alert(chulitoken);
        // alert(res);
        console.log(res);
        console.log(chulitoken);
        // if (res.Code == 200) {
        //   alert(res);
        //   console.log("6666666666666");
        //   setTimeout(async () => {
        //     await service.logout(res_token.refresh_token);
        //     window.location.href = window.location.origin;
        //   }, 100);
        // }
        window.location.href = "/welcome";
      })
      .catch(() => {
        // alert(console.error());
        service.logout(res_token.refresh_token);
        window.location.href = "/login";
      });
  });
}
accessToken();
// async function keycloakAuth() {
//   keycloak
//     .init({ onLoad: "check-sso", checkLoginIframe: false })
//     .then(async authenticated => {
//       if (authenticated) {
//         console.warn("User is authenticated");
//         service.getUserProfile(keycloak.token || "").then(res => {
//           console.warn("User Token Refresh");
//           window.location.href = "/welcome";
//         });
//       } else {
//         console.warn("User not authenticated");
//         keycloak.login();
//       }
//     })
//     .catch(err => {
//       console.error("Failed to initialize Keycloak", err);
//     });
// }

// keycloakAuth()

// async function handleLogout() {
//   if (keycloak.authenticated) {
//     keycloak.clearToken();
//     keycloak.authenticated = false;
//     keycloak
//       .logout({ redirectUri: `${window.location.origin}/login` })
//       .then(() => {
//         console.log("logout success");
//       })
//       .catch((err: any) => {
//         console.log("logout fail", err);
//       });
//   }
// }
</script>

<template>
  <div class="center-content">
    {{ "正在跳转....." }}
  </div>
</template>

<style lang="scss" scoped>
.center-content {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
</style>
